package com.stuManSystem.views;

import com.stuManSystem.base.BaseDao;
import com.stuManSystem.config.AppConstants;
import com.stuManSystem.config.Dao;
import com.stuManSystem.dao.AdminDao;
import com.stuManSystem.dao.StudentDao;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//方法一：通过集成将本类变成窗体类
public class LoginView extends JFrame{
    private JLabel name,pwd;

    private JTextField username;
    private JPasswordField password;
    private JButton login,reset,regist;

    private JPanel jPanelCenter, jPanelSouth;


    public LoginView(){
        init();
    }
    public void init(){
        setTitle(AppConstants.LoGIN_TITLE);
        jPanelCenter = new JPanel();
        jPanelCenter.setLayout(new GridLayout(3,2));

        name = new JLabel(AppConstants.LOGIN_USERNAME);
        pwd = new JLabel(AppConstants.LOGIN_PASSWORD);
        username = new JTextField();
        password = new JPasswordField();

        jPanelCenter.add(name);
        jPanelCenter.add(username);
        jPanelCenter.add(pwd);
        jPanelCenter.add(password);
        jPanelCenter.add(new JLabel("--------------------------------------------"));
        jPanelCenter.add(new JLabel("--------------------------------------------"));

        jPanelSouth = new JPanel(new GridLayout(1,3));

        login = new JButton(AppConstants.LOGIN);
        reset = new JButton(AppConstants.RESET);
        regist = new JButton("注册");

        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String uname = username.getText();
                String pword = String.valueOf(password.getPassword());
                //((AdminDao) BaseDao.getAbilityDAO(Dao.AdminDao)).queryForLogin(uname, pword);
                if(uname.equals("")||pword.equals("")){
                    JOptionPane.showConfirmDialog(LoginView.this,"请输入用户名和密码","提示",JOptionPane.DEFAULT_OPTION);
                }else {
                    if(((AdminDao) BaseDao.getAbilityDAO(Dao.AdminDao)).queryForLogin(uname,pword)){
                        new MainView();
                        dispose();
                    }
                }

            }
        });
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                username.setText("");
                password.setText("");
            }
        });
        regist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("注册");
                new RegistView();
            }
        });


        jPanelSouth.add(login);
        jPanelSouth.add(reset);
        jPanelSouth.add(regist);

        this.add(jPanelCenter,BorderLayout.CENTER);
        this.add(jPanelSouth,BorderLayout.SOUTH);

        //设置出现的位置和大小
        this.setBounds(450, 250, 375, 140);
        //窗体对象，默认是不可见的。通过设置setVisible，使其可见。
        this.setVisible(true);
        //拒绝改变窗体大小
        this.setResizable(false);
        //设置关闭窗体时的运行效果
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {
        new LoginView();
    }

    //设置属性


}
